﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace sieuthi
{
    public partial class gianhang : Form
    {
        private SqlDataAdapter da = new SqlDataAdapter();
        private SqlConnection conn = new SqlConnection(Threelayer.VO.core.connectionsring.ToString());
        private SqlCommand cmd = new SqlCommand();
        private DataTable dt = new DataTable("gianhang");
        Threelayer.VO.gianhang gh_vo;
        Threelayer.BUS.gianhangBUS gh_bus;
        public gianhang()
        {
            InitializeComponent();
            gianhangdatabinding();
        }

        private void gianhang_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'sieuthiDataSet.gianhang' table. You can move, or remove it, as needed.
            this.gianhangTableAdapter.Fill(this.sieuthiDataSet.gianhang);

        }
        private void gianhangdatabinding()
        {
            box_magh.DataBindings.Clear();
            box_magh.DataBindings.Add("text", dataGridgianhang.DataSource, "magh");
            Box_manql.DataBindings.Clear();
            Box_manql.DataBindings.Add("text", dataGridgianhang.DataSource, "manql");
            box_tengh.DataBindings.Clear();
            box_tengh.DataBindings.Add("text", dataGridgianhang.DataSource, "tengh");
            box_vitri.DataBindings.Clear();
            box_vitri.DataBindings.Add("text", dataGridgianhang.DataSource, "vitri");
        }
        private void data_gianhang()
        {
            gh_vo = new Threelayer.VO.gianhang();
            gh_vo.MaGH = box_magh.Text;
            gh_vo.MaNql = Box_manql.Text;
            gh_vo.TenGH = box_tengh.Text;
            gh_vo.Vitri = box_vitri.Text;
        }

        private void bt_gianhang_them_Click(object sender, EventArgs e)
        {
            data_gianhang();
            gh_bus = new Threelayer.BUS.gianhangBUS();
            gh_bus.gianhang_them(gh_vo);
            gianhang_Load(sender, e);
            gianhangdatabinding();
        }

        private void bt_gianhang_sua_Click(object sender, EventArgs e)
        {
            data_gianhang();
            gh_bus = new Threelayer.BUS.gianhangBUS();
            gh_bus.gianhang_sua(gh_vo);
            gianhang_Load(sender, e);
            gianhangdatabinding();
        }

        private void bt_gianhang_xoa_Click(object sender, EventArgs e)
        {
            data_gianhang();
            gh_bus = new Threelayer.BUS.gianhangBUS();
            gh_bus.gianhang_xoa(box_gianhang_timkiem.Text);
            gianhang_Load(sender, e);
            gianhangdatabinding();
        }

        private void bt_gianhang_timkiem_Click(object sender, EventArgs e)
        {
            dt.Clear();
            cmd.Dispose();
            cmd.Parameters.Clear();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            cmd.CommandText = "gianhang_timkiem";
            cmd.Parameters.Add("@magh", SqlDbType.NVarChar, 200).Value = box_gianhang_timkiem.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.ExecuteNonQuery();
            da.Fill(dt);
            dataGridgianhang.DataSource = dt;
            conn.Close();
        }
    }
}
